const API_BASE_URL = 'https://h501.zhusiot.com/';

/**
 * 封封微信的的request
 */
function requestByJson(url, data = {}) {
  return new Promise(function (resolve, reject) {
    wx.request({
      url:API_BASE_URL+ url,
      data: data,
      dataType: 'json',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.statusCode + '/' + res.errMsg);
        }
      },
      fail: function (err) {
        reject(err)
      }
    })
  });
}

function requestByUpload(url, data = {}, fPath) {
    return new Promise(function (resolve, reject) {
        wx.uploadFile({
            url: API_BASE_URL + url,
            header: {
                'content-type': 'multipart/form-data' // 默认值
            },
            filePath: fPath,
            name: 'file',
            formData: data,
            success(res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    reject(res.statusCode + '/' + res.errMsg);
                }
            },
            fail(err) {
                reject(err)
            }
        })
    });
}

module.exports = {
    requestByJson,
    requestByUpload,
}